/*
 * Copyright (C) 2011-2012 5ifilm.net All Rights Reserved.
 *
 * $Id: CacheUpdService.java 2012-08-09 WangJun$
 *
 */
package net.ifilm.service;

import net.ifilm.common.Constants;
import net.ifilm.common.cache.ICache;
import net.ifilm.dao.FilmDao;
import net.ifilm.dao.FilmTypeDao;
import net.ifilm.dao.SlideFilmDao;
import net.ifilm.dao.SysParaDao;
import net.ifilm.entity.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 *
 * User: <a href="wangjunneil@gmail.com">wangjun</a>
 * Date: 2012-08-09 22:40
 */
@Component
public class CacheUpdService {

    /**
     * 日志对象引用
     */
    private static Log log = LogFactory.getLog(CacheUpdService.class);

    @Autowired
    private SysParaDao sysParaDao;

    @Autowired
    private FilmDao filmDao;

    @Autowired
    private SlideFilmDao slideFilmDao;

    @Autowired
    private FilmTypeDao filmTypeDao;

    @Autowired
    private ICache<Object, Object> defaultCache;

    // --------------------------------------------------------------------------- public method

    /**
     * 更新全局系统参数表<tt>systemparam</tt>
     *
     * 以sysparamname为key，SystemParam对象为value存储于全局缓存中
     */
    public void updSysParamList() {
        Map<String, Object> parameter = new HashMap<String, Object>();
        parameter.put("activeFlag", Constants.ENABLE); // 查询有效状态的系统初始参数

        Map<String, SystemParam> systemParamMap = sysParaDao.querySysParamMap(parameter);
        setCache(Constants.CACHE_KEY_SYSPARAM, systemParamMap);
        if (log.isInfoEnabled()) {
            log.info("update system param list complate , the size is " + systemParamMap.size());
        }
    }

    /**
     * 更新幻灯影片表<tt>slidefilm</tt>数据
     *
     * 幻灯影片表数据只取5条有效数据放进全局缓存中，slidefilm表中必须有且只有
     * 5条有效数据，多一条少一条都视为配置错误。
     */
    public void updSlideFilmList() {
        Map<String, Object> parameter = new HashMap<String, Object>();
        parameter.put("activeFlag", Constants.ENABLE);
        parameter.put("limit", Constants.SLIDEFILM_NUM);
        parameter.put("sortColumn", "createtime");
        List<SlideFilm> slideFilmList = slideFilmDao.querySlideFilmList(parameter);
        setCache(Constants.CACHE_KEY_SLIDE, slideFilmList);

        log.info(slideFilmList);
    }

    /**
     * 缓存所有电影类型表数据
     */
    public void updFilmType() {
        List<FilmType> filmTypeList = filmDao.queryFilmTypeList();
        setCache(Constants.CACHE_KEY_FILMTYPE, filmTypeList);

        log.info(filmTypeList);
    }

    /**
     * 缓存电影发行地区列表
     */
    public void updRegion() {
        List<Region> regionList = filmDao.queryRegionList();
        setCache(Constants.CACHE_KEY_REGION, regionList);

        log.info(regionList);
    }



    /**
     * 更新全部缓存列表
     */
    public void updAll() {
        updSysParamList();
        updSlideFilmList();
    }


    // --------------------------------------------------------------------------- private method

    /**
     * 缓存装配方法
     *
     * @param key   键
     * @param value 值
     */
    private void setCache(Object key, Object value) {
        if (key == null) {
            throw new IllegalArgumentException("Invalid argument key");
        }

        defaultCache.put(key, value);
    }

    public ICache<Object, Object> getDefaultCache() {
        return this.defaultCache;
    }

} ///:~
